//
//  CarListController.m
//  HuLaQuan
//
//  Created by hok on 3/16/16.
//  Copyright © 2016 yuwubao. All rights reserved.
//

#import "CarListController.h"

// Cells
#import "CarListCell.h"

//Logic Model
#import "GetCarInfoEngine.h"

@interface CarListController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *addButton;

/**
 *  车辆列表
 */
@property (strong,nonatomic) NSMutableArray *carList;
/**
 *  选中的车辆
 */
@property (strong,nonatomic) NSIndexPath *selectedIndexPath;

@property (nonatomic,strong) GetCarInfoEngine *getCarInfoEngine;
@end

static NSString * const CarListCellIdentifier = @"CarListCell";

@implementation CarListController

- (GetCarInfoEngine *)getCarInfoEngine
{
    if (!_getCarInfoEngine) {
        _getCarInfoEngine = [[GetCarInfoEngine alloc] init];
    }
    
    return _getCarInfoEngine;
}
- (NSMutableArray *)carList
{
    if (!_carList) {
        _carList = [[NSMutableArray alloc] init];
    }
    return _carList;
    
}

- (void)setupUI
{
    [self setupNavBackButton];
    
    self.tableView.backgroundColor = UIColorFromRGBA(kBackgroundColor, 1.0);
    
    
    [self setupFooterForTableView];
    
    [self.tableView registerNib:[CarListCell nib] forCellReuseIdentifier:CarListCellIdentifier];
    
    
    
    self.title = @"车辆信息";
    
    
        [self.carList addObject:@{@"bankName":@"工商银行",@"cardType":@"储蓄32",@"cardNo":@"1897098723"}];
    //    [self.cardList addObject:@{@"bankName":@"工商银行",@"cardType":@"储蓄12",@"cardNo":@"1897098723"}];
    
    [self updateUI];
    
    [self createNavigationBarRightBarButtonItemWithTitle:@"增加"];
}
- (void)updateUI
{
    [self.tableView  setHidden:self.carList.count ? NO:YES];
    [self.addButton setHidden:self.carList.count ? YES:NO];
    
    [self.tableView reloadData];
    
}
- (void)rightAction
{
    NSLog(@"编辑");
    [self tapAddButton:nil];
}
- (IBAction)tapAddButton:(id)sender {
    
    [self performSegueWithIdentifier:@"ShowAddCar" sender:self];
}
- (void)setupFooterForTableView
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,1)];
    self.tableView.tableFooterView = v;
}


#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return kCarListCellHeight;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.carList.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kUserCenterSectionHeaderHeight;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CarListCell *cell = [tableView dequeueReusableCellWithIdentifier:CarListCellIdentifier
                                                             forIndexPath:indexPath];
//    cell.cardBank.text = self.cardList[indexPath.row][@"bankName"];
//    
//    NSString *type = self.cardList[indexPath.row][@"accType"];
//    cell.cardType.text = [self cardType:type];
//    
//    cell.cardNumber.text = [self formCardNumberToShow:self.cardList[indexPath.row][@"cardId"]];
//    
//    cell.cardImageName = self.cardList[indexPath.row][@"bankName"];
    return cell;
    
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.selectedIndexPath = indexPath;
    [self performSegueWithIdentifier:@"ShowCarDetailInfo" sender:self];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
